<?php


namespace App\Services;


use App\Models\Estate;
use App\Models\HouseFeeConfig;
use App\Models\Meter;
use App\Models\Owner;
use App\Models\OwnerItem;

class MeterService
{
    /**
     * 初始化抄表数据
     * @param Estate $estate 实现了房地产接口的实例
     * @param $water_meter  水表初始值
     * @param $electric_meter 电表初始值
     */
    public function initMeter(Estate $estate, $water_meter = 0, $electric_meter = 0)
    {
        // 插入基础电，水表数据
        Meter::query()->create([
            'value' => $water_meter,
            'house_fee_config_id' => HouseFeeConfig::query()->where('name', '水')->first()->id,
            'count' => 1,
            'meterable_id' => $estate->id,
            'meterable_type' => get_class($estate)
        ]);
        Meter::query()->create([
            'value' => $electric_meter,
            'house_fee_config_id' => HouseFeeConfig::query()->where('name', '电')->first()->id,
            'count' => 1,
            'meterable_id' => $estate->id,
            'meterable_type' => get_class($estate)
        ]);
    }

    /**
     * 抄表
     * @param Meter $meter 上次抄表的对象
     * @param $value       这次抄表的读数
     */
    public function meterReading(Meter $lastMeter, $value)
    {
        // 根据上次抄表数据增加
        Meter::query()->create([
            'value' => $value,
            'house_fee_config_id' => $lastMeter->house_fee_config_id,
            'count' => $lastMeter->count + 1,
            'meterable_id' => $lastMeter->meterable_id,
            'meterable_type' => $lastMeter->meterable_type
        ]);
    }

    /**
     * 根据合同获取到最新（最后一次）的抄表对象
     * @param Owner $owner
     * @param HouseFeeConfig $config
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function getLastMeterByOwner(Owner $owner, HouseFeeConfig $config)
    {
        return Meter::query()->where('meterable_id', $owner->ownerable_id)
            ->where('meterable_type', $owner->ownerable_type)
            ->where('house_fee_config_id', $config->id)
            ->orderBy('count', 'desc')
            ->first();
    }

    /**
     * 根据合同获取到到时第二次的抄表对象
     * @param Owner $owner
     * @param HouseFeeConfig $config
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Query\Builder|object|null
     */
    public function getLastLastMeterByOwner(Owner $owner, HouseFeeConfig $config)
    {
        return Meter::query()->where('meterable_id', $owner->ownerable_id)
            ->where('meterable_type', $owner->ownerable_type)
            ->where('house_fee_config_id', $config->id)
            ->orderBy('count', 'desc')
            ->skip(1)
            ->first();
    }

    /**
     * 根据流水获取到最新（最后一次）的抄表对象
     * 存在问题：抄表的数据表，第一条数据是没有关联流水的。
     * @param OwnerItem $ownerItem
     * @param HouseFeeConfig $config
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function getLastMeterByOwnerItem(OwnerItem $ownerItem, HouseFeeConfig $config)
    {
        return Meter::query()->where('owner_item', $ownerItem->id)
            ->where('house_fee_config_id', $config->id)
            ->orderBy('count', 'desc')
            ->first();
    }

    /**
     * 获取上一期的meter
     * @param Meter $meter
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * @throws \Exception
     */
    public function getLastMeterByNowMeter(Meter $meter)
    {
        $count = $meter->count - 1;
        if ($count < 0) {
            throw new \Exception("参数异常：count属性必须大于0");
        }
        return Meter::query()->where('owner_item', $meter->id)
            ->where('house_fee_config_id', $meter->id)
            ->where('count', $count)
            ->orderBy('count', 'desc')
            ->first();
    }
}